package com.yhf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhf.mapper.SysUserMapper;
import com.yhf.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyUserSecurityService implements UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //校验用户名
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);

        if (sysUser==null){
            System.out.println("用户名或者密码错误");
            return null;
        }
        //授权
        List<GrantedAuthority> list = new ArrayList<>();
        return new org.springframework.security.core.userdetails.User(username,sysUser.getPassword(),list);
    }
}
